//
//  ViewController.m
//  PopUpDemo
//
//  Created by mac on 2025/9/4.
//
//  主ViewController
//  弹出窗的集中实现方式

#import "ViewController.h"
#import "AppDelegate.h"
#import "PopupViewController.h"

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // Do any additional setup after loading the view.
}


- (void)setRepresentedObject:(id)representedObject {
    [super setRepresentedObject:representedObject];

    // Update the view, if already loaded.
}

- (void)terminateApplication {
    [NSApp terminate:nil];
}

- (IBAction)openByCode:(id)sender {
    NSStoryboard *storyboard = [NSStoryboard storyboardWithName:@"Main" bundle:nil];
    NSWindowController *windowController = [storyboard instantiateControllerWithIdentifier:@"PopupWindow"];
    NSViewController *loginViewController = [storyboard instantiateControllerWithIdentifier:@"PopupVC"];
    windowController.contentViewController = loginViewController;
    //[self presentViewControllerAsSheet:loginViewController];
    
    AppDelegate *appDelegate = (AppDelegate *)[NSApplication sharedApplication].delegate;
    appDelegate.mainWindowController = self.view.window.windowController;
    
    NSModalResponse response = [NSApp runModalForWindow:windowController.window];
    if (response == NSModalResponseOK) {
        // 处理用户点击“确定”等操作
        NSLog(@"点击了确定");
        //[self.view.window makeKeyAndOrderFront:self];
    } else {
        // 处理用户取消等操作
        NSLog(@"点击了取消");
        //[self.view.window makeKeyAndOrderFront:self];
        //[NSApp terminate:self];
    }
}

- (IBAction)presentAsSheet:(id)sender {
    NSStoryboard *storyboard = [NSStoryboard storyboardWithName:@"Main" bundle:nil];
    NSWindowController *windowController = [storyboard instantiateControllerWithIdentifier:@"PopupWindow"];
    PopupViewController *popupViewController = [storyboard instantiateControllerWithIdentifier:@"PopupVC"];
    windowController.contentViewController = popupViewController;
    __weak typeof(self) weakSelf = self;
    popupViewController.terminationHandler = ^{
        [weakSelf terminateApplication];
    };
    [self presentViewControllerAsSheet:popupViewController];
}


@end
